<template>
  <div>
    <!-- 导航 -->
    <Nav>
      <el-breadcrumb-item
        slot="nav"
        v-for="(item,index) in breadcrumbList"
        :key="index"
        :to="{ path : item.href}"
      >{{item.name}}</el-breadcrumb-item>
      <SBtn></SBtn>
    </Nav>
    <!-- 卡片主题内容 -->
    <el-card>
      <!-- 搜索和按钮 -->
      <el-row>
        <el-input
          placeholder="请输入内容"
          width="300px"
          v-model="querInfo.query"
          clearable
          @clear="getGoodsList"
        >
          <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
        </el-input>
        <el-button type="primary" @click="Addgoods">添加商品</el-button>
      </el-row>
      <!-- 表单 -->
      <el-table :data="goodsData" style="width: 100%" border>
        <el-table-column type="index" label="#" width="80"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="150"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="150"></el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="150">
          <template slot-scope="scope">{{scope.row.add_time | dateFormat}}</template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button type="warning" icon="el-icon-edit" size="mini">编辑</el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="delGoods(scope.row.goods_id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页器 -->
      <!-- <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="querInfo.pagenum"
        :page-sizes="[1, 2, 3, 40]"
        :page-size="querInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>-->
      <Pagination
        :handleSizeChange="handleSizeChange"
        :handleCurrentChange="handleCurrentChange"
        :querInfo="querInfo"
        :total="total"
      ></Pagination>
    </el-card>
  </div>
</template>

<script>
import Nav from '../nav'
import Pagination from '../pagination'
export default {
  data() {
    return {
      breadcrumbList: [
        {
          name: '首页',
          href: '/welcome',
        },
        {
          name: '商品管理',
        },
        {
          name: '商品列表',
        },
      ],
      goodsData: [],
      querInfo: {
        query: '',
        // 当前页码
        pagenum: 1,
        // 每页显示的条数
        pagesize: 5,
      },
      total: 0,
    }
  },
  methods: {
    //   获取商品列表数据
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', {
        params: this.querInfo,
      })
      if (res.meta.status !== 200)
        return this.$message.error('获取商品列表数据失败')
      this.goodsData = res.data.goods
      this.total = res.data.total
    },

    handleSizeChange(newSize) {
      this.querInfo.pagesize = newSize
      this.getGoodsList()
    },
    // 当前页码发生该变
    handleCurrentChange(newPage) {
      this.querInfo.pagenum = newPage
      this.getGoodsList()
    },

    // 点击添加商品
    Addgoods() {
      console.log('a')
      this.$router.push('/addGoods')
    },

    //删除商品
    async delGoods(id) {
      const { data: res } = await this.$http.delete(`goods/${id}`)
      if (res.meta.status !== 200) return this.$message.error('删除失败')
      this.$message.success('删除成功')
      this.getGoodsList()
    },
  },
  created() {
    this.getGoodsList()
  },
  components: {
    Nav,
    Pagination,
  },
}
</script>

<style lang="less" scoped>
.el-input {
  width: 400px;
  margin-right: 20px;
}
</style>